package com.api.league.service.impl;

import com.api.base.utils.PageInfoUtils;
import com.api.core.service.AbstractService;
import com.api.league.dao.LeagueTransferMapper;
import com.api.league.form.LeagueTransferForm;
import com.api.league.model.LeagueTransferRecord;
import com.api.league.service.LeagueTransferService;
import com.api.league.vo.LeagueTransferRecordVo;
import com.api.user.service.TeamService;
import com.api.user.service.UserInfoService;
import com.api.user.vo.TeamVo;
import com.api.user.vo.UserInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author RenQiang
 * @date 2021/6/2
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LeagueTransferServiceImpl extends AbstractService<LeagueTransferRecord> implements LeagueTransferService {
    private final LeagueTransferMapper leagueTransferMapper;
    private final UserInfoService userInfoService;
    private final TeamService teamService;

    public LeagueTransferServiceImpl(LeagueTransferMapper leagueTransferMapper, UserInfoService userInfoService, TeamService teamService) {
        this.leagueTransferMapper = leagueTransferMapper;
        this.userInfoService = userInfoService;
        this.teamService = teamService;
    }

    @Override
    public PageInfo<LeagueTransferRecordVo> findByPage(Long leagueId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<LeagueTransferRecord> transferRecords = leagueTransferMapper.findByPage(leagueId);
        PageInfo<LeagueTransferRecord> pageInfo = new PageInfo<>(transferRecords);

        List<Long> userIds = transferRecords.stream()
                .map(LeagueTransferRecord::getPlayerId)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, UserInfoVo> userIdVoMap = userInfoService.getIdVoMap(userIds);

        List<Long> teamIds = transferRecords.stream()
                .flatMap(record -> Stream.of(record.getExClubId(), record.getNewClubId()))
                .distinct()
                .collect(Collectors.toList());
        Map<Long, TeamVo> teamIdVoMap = teamService.getIdVoMap(teamIds);

        List<LeagueTransferRecordVo> recordVos = transferRecords.stream()
                .map(record -> {
                    LeagueTransferRecordVo recordVo = new LeagueTransferRecordVo();
                    BeanUtils.copyProperties(record, recordVo);
                    recordVo.setPlayer(userIdVoMap.get(record.getPlayerId()));
                    recordVo.setNewClub(teamIdVoMap.get(record.getNewClubId()));
                    recordVo.setExClub(teamIdVoMap.get(record.getExClubId()));
                    return recordVo;
                })
                .collect(Collectors.toList());
        return PageInfoUtils.convertPageInfo(pageInfo, recordVos);
    }

    @Override
    public void batchCreateTransferRecords(List<LeagueTransferForm> forms) {
        List<LeagueTransferRecord> transferRecords = forms.stream()
                .map(form -> {
                    LeagueTransferRecord transferRecord = new LeagueTransferRecord();
                    transferRecord.setLeagueId(form.getLeagueId());
                    transferRecord.setPlayerId(form.getPlayerId());
                    transferRecord.setNewClubId(form.getNewClubId());
                    transferRecord.setExClubId(form.getExClubId());
                    transferRecord.setTransferTime(new Date());
                    transferRecord.setTransferFee(form.getTransferFee());
                    return transferRecord;
                })
                .collect(Collectors.toList());
        leagueTransferMapper.insertList(transferRecords);
        for (LeagueTransferForm form : forms) {
            teamService.moveto(form.getExClubId(), form.getPlayerId(), form.getNewClubId());
        }
    }
}
